<!DOCTYPE html>
<html lang=en>
<head>
  <meta charset=utf-8>
  <meta http-equiv=X-UA-Compatible content="IE=edge">
  <meta id=viewport name=viewport content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.min.js"></script>
</head>
<body>

<div id="app1">
<button id="test">getAuthCode</button>
<button id="pay">支付</button>
<br /><br />
<div id="code-id"></div>
</div>

<script>

function queryWithUrl() {
    const arr1 = location.href.split('?')
    if (arr1.length <= 1) { return {} }
    const queryStr = arr1[1]
    const queryArr = queryStr.split('&')
    const query = {}
    queryArr.forEach((item) => {
      const [key, val] = item.split('=')
      query[key] = val
    })
    return query
}

var queryData = queryWithUrl()

ap.alert(JSON.stringify(queryData));
document.querySelector('#test').addEventListener('click', function() {
  var query = {
        appId: queryData.appId,
        scopes: ['auth_base'],
        showErrorTip: false
      }
  ap.alert({
      title: '参数信息',
      content: JSON.stringify(query),
      buttonText: '我知道了'
    }, function(){
      ap.getAuthCode(query, function(res){
        document.getElementById('code-id').innerText = JSON.stringify(res)
        ap.alert(JSON.stringify(res));
      });
    });

});
document.querySelector('#pay').addEventListener('click', function() {
  ap.tradePay({
      tradeNO: queryData.tradeNO
    }, function(res){
      ap.alert(res.resultCode);
    });
});
</script>

</body>
</html>
